Laro.register('VHome.$states', function (La) {
    var pkg = this;

    this.No = La.BaseState.extend(function () {

    }).methods({
        enter: function (msg, fromState) {
            this._t = 0;
            VHome.addNormalBalls();
        },
        leave: function () {

        },
        update: function (dt) {
            this._t += dt;
            VHome.updateBalls(dt, 'normal');
        },
        transition: function () {
            if (this._t > 2) {
                this.host.setState(1);
            }
        },
        draw: function () {

        }
    });

    this.QQ = La.BaseState.extend(function () {

    }).methods({
        enter: function (msg, fromState) {
            for (var i = 0; i < VHome.qqParticles.length; i ++) {
                var ball = VHome.qqParticles[i];
                VHome.stage.addChild(ball);
                ball.end = false;
                ball.width = ball.logoPos.width;
                ball.startAnimTime = (+ new Date);

            }
            this._t = 0;
            this.explosion = false;
        },
        leave: function () {
            for (var i = 0; i < VHome.stage.children.length; i ++) {
                var ball = VHome.stage.children[i];
                if (ball.type == 'normal') {
                    VHome.stage.children.splice(i, 1);
                    i --;
                }
            }
        },
        update: function (dt) {
            this._t += dt;
            VHome.updateBalls(dt, 'normal');

            for (var i = 0; i < VHome.qqParticles.length; i ++) {
                var ball = (VHome.qqParticles[i]);

                VHome.tween(ball, 1000);
                ball.zpos += VHome.zstep;

                ball.rotateX(VHome.angleX);
                ball.rotateY(VHome.angleY);
                var scale = ball.getScale(),
                pos = ball.getScreenXY();

                ball.width = Math.max(10*scale, 2);
                ball.x = pos.x;
                ball.y = pos.y;
            }
        },
        transition: function () {
            var range = VHome.range,
                vpx = VHome.vpx,
                vpy = VHome.vpy;
            if (this._t > 5 && !this.explosion) {
                for (var i = 0; i < VHome.qqParticles.length; i ++) {
                    var ball = VHome.qqParticles[i];

                    ball.f_xpos = ball.xpos;
                    ball.f_ypos = ball.ypos;
                    ball.f_zpos = ball.zpos;
                    ball.t_xpos = range(-vpx, vpx);
                    ball.t_ypos = range(-vpy, vpy);
                    ball.t_zpos = range(-vpx, vpx);

                    ball.end = false;
                    ball.width = range(8, 15);
                    ball.startAnimTime = (+ new Date);
                }
                this.explosion = true;
                this.explosionT = (+new Date);
            }
            if (this.explosion && (+new Date) - this.explosionT >= 1000) {
                this.host.setState(2);
            }
        },
        draw: function () {

        }
    });

    this.Jx = La.BaseState.extend(function () {

    }).methods({
        enter: function (msg, fromState) {
            this.push = false;
            this._t = 0;
            this.explosion = false;
        },
        leave: function () {
            for (var i = 0; i < VHome.stage.children.length; i ++) {
                var ball = VHome.stage.children[i];
                if (ball.type == 'qq') {
                    VHome.stage.children.splice(i, 1);
                    i --;
                }
            }
        },
        update: function (dt) {
            this._t += dt;
            VHome.updateBalls(dt, 'qq');

            if (this._t > 2 && !this.push) {
                VHome.pushBalls('jx');
                this.push = true;
            }
            if (this.push) {
                for (var i = 0; i < VHome.jxParticles.length; i ++) {
                    var ball = (VHome.jxParticles[i]);

                    VHome.tween(ball, 1000);
                    ball.zpos += VHome.zstep;

                    ball.rotateX(VHome.angleX);
                    ball.rotateY(VHome.angleY);
                    var scale = ball.getScale(),
                    pos = ball.getScreenXY();

                    ball.width = Math.max(10*scale, 2);
                    ball.x = pos.x;
                    ball.y = pos.y;
                }
            }

            // explosion
            this.checkExplosion();
        },
        checkExplosion: function () {
            var range = VHome.range,
                vpx = VHome.vpx,
                vpy = VHome.vpy;
            if (this._t > 5 && !this.explosion) {
                for (var i = 0; i < VHome.jxParticles.length; i ++) {
                    var ball = VHome.jxParticles[i];

                    ball.f_xpos = ball.xpos;
                    ball.f_ypos = ball.ypos;
                    ball.f_zpos = ball.zpos;
                    ball.t_xpos = range(-vpx, vpx);
                    ball.t_ypos = range(-vpy, vpy);
                    ball.t_zpos = range(-vpx, vpx);

                    ball.end = false;
                    ball.width = range(8, 15);
                    ball.startAnimTime = (+ new Date);
                }
                this.explosion = true;
                this.explosionT = (+new Date);
            }
            if (this.explosion && (+new Date) - this.explosionT >= 1000) {
                this.host.setState(3);
            }
        },
        draw: function () {

        }
    });

    this.QPlus = La.BaseState.extend(function () {

    }).methods({
        enter: function (msg, fromState) {
            this.push = false;
            this._t = 0;
            this.explosion = false;
        },
        leave: function () {
            for (var i = 0; i < VHome.stage.children.length; i ++) {
                var ball = VHome.stage.children[i];
                if (ball.type == 'jx') {
                    VHome.stage.children.splice(i, 1);
                    i --;
                }
            }
        },
        update: function (dt) {
            this._t += dt;
            VHome.updateBalls(dt, 'jx');

            if (this._t > 2 && !this.push) {
                VHome.pushBalls('qplus');
                this.push = true;
            }

            if (this.push) {
                for (var i = 0; i < VHome.qplusParticles.length; i ++) {
                    var ball = (VHome.qplusParticles[i]);

                    VHome.tween(ball, 1000);
                    ball.zpos += VHome.zstep;

                    ball.rotateX(VHome.angleX);
                    ball.rotateY(VHome.angleY);
                    var scale = ball.getScale(),
                    pos = ball.getScreenXY();

                    ball.width = Math.max(10*scale, 2);
                    ball.x = pos.x;
                    ball.y = pos.y;
                }
            }
            this.checkExplosion();

        },
        checkExplosion: function () {
            var range = VHome.range,
                vpx = VHome.vpx,
                vpy = VHome.vpy;
            if (this._t > 5 && !this.explosion) {
                for (var i = 0; i < VHome.qplusParticles.length; i ++) {
                    var ball = VHome.qplusParticles[i];

                    ball.f_xpos = ball.xpos;
                    ball.f_ypos = ball.ypos;
                    ball.f_zpos = ball.zpos;
                    ball.t_xpos = range(-vpx, vpx);
                    ball.t_ypos = range(-vpy, vpy);
                    ball.t_zpos = range(-vpx, vpx);

                    ball.end = false;
                    ball.width = range(8, 15);
                    ball.startAnimTime = (+ new Date);
                }
                this.explosion = true;
                this.explosionT = (+new Date);
            }
            if (this.explosion && (+new Date) - this.explosionT >= 1000) {
                this.host.setState(4);
            }
        },
        transition: function () {

        },
        draw: function () {

        }
    });

    this.AT = La.BaseState.extend(function () {

    }).methods({
        enter: function (msg, fromState) {
            this.push = false;
            this._t = 0;
            this.explosion = false;
        },

        leave: function () {

        },
        update: function (dt) {
            this._t += dt;
            VHome.updateBalls(dt, 'qplus');

            if (this._t > 2 && !this.push) {
                VHome.pushBalls('at');
                this.push = true;
            }

            if (this.push) {
                for (var i = 0; i < VHome.atParticles.length; i ++) {
                    var ball = (VHome.atParticles[i]);

                    VHome.tween(ball, 1000);
                    ball.zpos += VHome.zstep;

                    ball.rotateX(VHome.angleX);
                    ball.rotateY(VHome.angleY);
                    var scale = ball.getScale(),
                    pos = ball.getScreenXY();

                    ball.width = Math.max(10*scale, 2);
                    ball.x = pos.x;
                    ball.y = pos.y;
                }
            }
            //this.checkExplosion();
        },
        transition: function () {
            if (this._t > 7 && !this.tryJump) {
                window.location.href = 'https://github.com/vincenthou';
                this.tryJump = true;
            }
        },
        draw: function () {

        }
    });
});
